package com.wuyan.locationdog.ui.my


import android.os.Bundle
import android.os.CountDownTimer
import androidx.activity.viewModels
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.ext.afterTextChange
import com.arcsoft.commonbase.ext.customShowLong
import com.arcsoft.commonbase.ext.hidePhoneNumber
import com.arcsoft.commonbase.ext.isTrimEmpty
import com.arcsoft.commonbase.ext.textStringTrim
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.databinding.MyModifyPwdPhoneActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.help.LoginHelp
import com.wuyan.locationdog.viewmodel.LoginViewModel

class MyModifyPwdPhoneActivity : BaseBindingActivity<MyModifyPwdPhoneActivityBinding>() {

    private lateinit var userPhone: String
    private val mViewModel: LoginViewModel by viewModels()

    override fun initData() {
        if (DeviceHelp.isDeviceModifyPwd) {
            setTitleText("修改设备登录密码")
            userPhone = DeviceHelp.selectDeviceBeanContent.deviceDetailVO.userPhone.toString()
        } else {
            setTitleText("修改密码")
            userPhone = LoginHelp.myInfoBean.content.userPhone
        }

        binding.apply {
            SpanUtils.with(userPhoneView)
                .append("点击获取验证码按钮，将会发送一条带有验证码的信息至手机")
                .setForegroundColor(ColorUtils.getColor(com.arcsoft.commonbase.R.color.color_3))
                .append("+86 " + hidePhoneNumber(userPhone))
                .setForegroundColor(ColorUtils.getColor(com.arcsoft.commonbase.R.color.text_color_selected))
                .create()
            btnMyMsgHelp.setOnClickListener {
                ActivityUtils.startActivity(MyMsgHelpActivity::class.java)
            }
            btnMsg.setOnClickListener {
                //类型 1注册 2修改密码 3登录 4修改手机号(旧手机号) 5修改手机号(新手机号) 6找回密码 7修改设备密码
                val map = HashMap<String, Any>()
                map["phone"] = userPhone
                if (DeviceHelp.isDeviceModifyPwd) {
                    map["deviceId"] = DeviceHelp.selectDeviceBeanContent.deviceDetailVO.deviceId
                    map["type"] = 7
                } else {
                    map["type"] = 2
                }

                mViewModel.getCode(map)
            }
            btnSure.setOnClickListener {
                val map = HashMap<String, Any>()
                map["phone"] = userPhone
                map["code"] = etMsg.textStringTrim()
                if (DeviceHelp.isDeviceModifyPwd) {
                    map["deviceId"] = DeviceHelp.selectDeviceBeanContent.deviceDetailVO.deviceId
                    map["type"] = 7
                } else {
                    map["type"] = 2
                }
                mViewModel.checkCode(map)
            }

            btnSure.alpha = 0.4F
            btnSure.isEnabled = false
            etMsg.afterTextChange {
                if (etMsg.isTrimEmpty()) {
                    btnSure.alpha = 0.4F
                    btnSure.isEnabled = false
                } else {
                    btnSure.alpha = 1F
                    btnSure.isEnabled = true
                }
            }
        }
    }

    private var countDownTimer: CountDownTimer? = null

    /**
     * 验证码倒计时
     */
    private fun MyModifyPwdPhoneActivityBinding.msgCountDownTimer() {
        if (countDownTimer == null) {
            countDownTimer = object : CountDownTimer(60 * 1000, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                    btnMsg.isEnabled = false
                    btnMsg.text = (millisUntilFinished / 1000).toString() + "s后重发"
                    btnMsg.setTextColor(ColorUtils.getColor(com.arcsoft.commonbase.R.color.text_color_unselected))
                }

                override fun onFinish() {
                    btnMsg.isEnabled = true
                    btnMsg.text = "获取验证码"
                    btnMsg.setTextColor(ColorUtils.getColor(com.arcsoft.commonbase.R.color.text_color_selected))

                }
            }
        }
        countDownTimer?.start()
    }

    override fun observe() {
        super.observe()

        mViewModel.yzmBean.observe(this) {
            customShowLong("成功")
            binding.msgCountDownTimer()
        }

        mViewModel.any.observe(this) {
            val bundle = Bundle()
            bundle.putString("code", binding.etMsg.textStringTrim())
            ActivityUtils.startActivity(bundle, MyModifyPwdInputActivity::class.java)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (countDownTimer != null) {
            countDownTimer?.cancel()
        }
    }

}